//
//  JYSettingController.m
//  JYFarm
//
//  Created by 花花 on 2017/10/12.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYSettingController.h"
#import "JYMineMenuCell.h"
#import "LYFielService.h"
#import "JYSetingPwdController.h"
#import "JYLoginController.h"
#import "BaseNavViewController.h"
#import "JYShowView.h"
@interface JYSettingController ()

@property(nonatomic)NSArray *titleArr;
@property(nonatomic)NSArray *imgArr;

@property(nonatomic)JYShowView *showView;

@end

static NSString *const JYSettingCellID = @"JYSettingCellID";
@implementation JYSettingController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initUI];

}
- (void)initUI{

    self.title = @"设置";
    self.titleArr = @[@[@"修改密码"],@[@"检查更新",@"清除缓存"]];
    self.imgArr = @[@[@"mine_setting_modification"],@[@"mine_setting_update",@"mine_setting_cache"]];
    
    [self.tableView registerClass:[JYMineMenuCell class] forCellReuseIdentifier:JYSettingCellID];
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, kBtnHeight+30)];
    UIButton *logoutBtn  =  [UIButton jy_buttontitle:@"退出当前账号" titleColor:k3Color frame:CGRectMake(10, 30, Screen_Width-20, kBtnHeight) target:self action:@selector(logoutOnClick) fontSize:17 bgImageName:@"logout"];
    [footerView addSubview:logoutBtn];
    self.tableView.tableFooterView = footerView;

}
#pragma mark - <UITableViewDataSource,UITableViewDelegate>
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
   if(section == 0){
        return [self.titleArr[0] count];
    }
    return [self.titleArr[1] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JYMineMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:JYSettingCellID];
    
    if (indexPath.section == 0) {
        cell.titleLabel.text = [self.titleArr[0] safeObjectAtIndex:indexPath.row];
        cell.iconImage.image =[UIImage imageNamed:[self.imgArr[0] safeObjectAtIndex:indexPath.row]];
        }else{
        cell.titleLabel.text = [self.titleArr[1] safeObjectAtIndex:indexPath.row];
        cell.iconImage.image =[UIImage imageNamed:[self.imgArr[1] safeObjectAtIndex:indexPath.row]];
            if (indexPath.row ==1) {
                cell.subMoenyLabel.text = [self getCacheSize];
                cell.subMoenyLabel.textColor = k3Color;
            }
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0 && indexPath.row == 0) {
        JYSetingPwdController *settingVc = [[JYSetingPwdController alloc]initWithIsSetingPwd:NO];
        [self.navigationController pushViewController:settingVc animated:YES];
    }
    
    if (indexPath.section == 1 && indexPath.row ==1) {
        
        [MBProgressHUD LY_ShowProgress:@"正在清除缓存"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD LY_HideHUD:YES];
        });
        [LYFielService clearCache:@"Library/Caches"];
        [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:1 inSection:1]] withRowAnimation:UITableViewRowAnimationFade];

    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section == 0) {
        return 0;
    }
    return 10;
    
}
#pragma mark - Custom Methods
- (NSString *)getCacheSize{
    
    double size_m = [LYFielService folderSizeAtPath:@"Library/Caches"];
    NSString *cacheSizeStr = [NSString stringWithFormat:@"%.2fMB",size_m];
    if ([cacheSizeStr isEqual:@"nanMB"]) {
        cacheSizeStr = @"0.00MB";
    }
    return cacheSizeStr;
    
}
- (void)logoutOnClick{

    NSLog(@"退出当前账号");
    
    
    
    JYWeakSelf;
    
    JYShowView *showView = [[JYShowView alloc]initWithFrame:CGRectMake(0, 0, showView_Width, showView_Height) isSected:YES alertMessage:@"删除后商品将不在显示，确认删除类商品吗?" btnHandler:^(JYShowType type) {
        if (type == JYShowTypeCancel) {
            
        }else{
            [weakSelf logoutNetworkRequest];
           
            JYLoginController *loginVc = [[JYLoginController  alloc]init];
            BaseNavViewController *nav = [[BaseNavViewController alloc]initWithRootViewController:loginVc];
            [self presentViewController:nav animated:YES completion:nil];
            
            
        }
    }];
    [showView show];
}

- (void)logoutNetworkRequest{

     [LH loginOutDataHandle];

}
@end
